package top.lyc829.zzl_android.ui.thing.dynamic;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.xuexiang.xui.widget.imageview.nine.ItemImageClickListener;
import com.xuexiang.xui.widget.imageview.nine.NineGridImageView;
import com.xuexiang.xui.widget.imageview.nine.NineGridImageViewAdapter;
import com.xuexiang.xui.widget.imageview.preview.PreviewBuilder;
import com.xuexiang.xui.widget.imageview.preview.loader.GlideMediaLoader;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import top.lyc829.zzl_android.R;
import top.lyc829.zzl_android.logic.bean.DynamicBean;
import top.lyc829.zzl_android.logic.bean.ImageViewInfo;

public class DynamicRecyclerViewAdapter extends BaseQuickAdapter<DynamicBean, BaseViewHolder> implements LoadMoreModule {

    //TODO: 九宫格图片有时候不加载

    NineGridImageView<String> mNgl;

    public DynamicRecyclerViewAdapter() {
        super(R.layout.dynamic_item);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, DynamicBean dynamicBean) {
        baseViewHolder
                .setText(R.id.my_dynamic_user_name, dynamicBean.getUserName())
                .setText(R.id.dynamic_item_create_time, dynamicBean.getCreateTime())
                .setText(R.id.dynamic_text, dynamicBean.getDynamicText())
                .setText(R.id.dynamic_replay_num, dynamicBean.getComments().toString())
                .setText(R.id.dynamic_praise_num, dynamicBean.getLikes().toString());
        Glide.with(getContext())
                .load(dynamicBean.getUserUrl())
                .into((ImageView) baseViewHolder.getView(R.id.my_dynamic_user_Url));

        mNgl = baseViewHolder.getView(R.id.dynamic_ngl_images);


        NineGridImageViewAdapter<String> mAdapter = new NineGridImageViewAdapter<String>() {
            @Override
            protected void onDisplayImage(Context context, ImageView imageView, String s) {
                Glide.with(imageView.getContext())
                        .load(s)
                        .apply(GlideMediaLoader.getRequestOptions())
                        .placeholder(R.drawable.loading)
                        .into(imageView);
            }

            @Override
            protected ImageView generateImageView(Context context) {
                return super.generateImageView(context);
            }
        };

        mNgl.setImagesData(dynamicBean.getDynamicUrl());
        mNgl.setAdapter(mAdapter);
        mNgl.setItemImageClickListener(new ItemImageClickListener<String>() {
            @Override
            public void onItemImageClick(ImageView imageView, int index, List<String> list) {
                List<ImageViewInfo> res = computeBoundsBackward(list);//组成数据
                PreviewBuilder.from((Activity) imageView.getContext())
                        .setImgs(res)
                        .setCurrentIndex(index)
                        .setProgressColor(R.color.zzl_red)
                        .setType(PreviewBuilder.IndicatorType.Dot)
                        .start();//启动
            }
        });



    }

    /**
     * 查找信息
     * @param list 图片集合
     */
    private List<ImageViewInfo> computeBoundsBackward(List<String> list) {
        List<ImageViewInfo> res = new ArrayList<>();
        for (int i = 0;i < mNgl.getChildCount(); i++) {
            View itemView = mNgl.getChildAt(i);
            Rect bounds = new Rect();
            if (itemView != null) {
                ImageView thumbView = (ImageView) itemView;
                thumbView.getGlobalVisibleRect(bounds);
            }
            ImageViewInfo info = new ImageViewInfo(list.get(i));
            info.setBounds(bounds);
            res.add(info);
        }

        return res;

    }
}

